package com.ujiuye.service.impl;

import com.ujiuye.dao.StudentDao;
import com.ujiuye.dao.impl.StudentDaoImpl;
import com.ujiuye.model.Page;
import com.ujiuye.model.Student;
import com.ujiuye.service.StudentService;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    private StudentDao sd = new StudentDaoImpl();
    @Override
    public boolean saveStudent(Student student) {
        // 将student对象中封装的数据交给数据交互层
        StudentDao sd = new StudentDaoImpl();
        int row = sd.saveStudent(student);
        return row > 0 ? true : false;
    }

    @Override
    public Page<Student> stuPageList(String currentPage, Student student) {
        long count = sd.getStuCount(student);
        int current = 1;
        if (currentPage != null && currentPage.trim() != ""){
            current = Integer.parseInt(currentPage);
        }
        //获取分页数据
        int size =5;  //每页显示条数
        /*
        1页 0     （1-1）*5
        2页 5   （2-1）*5
        3页 10    （3-1）*5
        n页  （n-1）*5
         */
        // 用当前页减1 乘以每页显示的条数
        int start =(current-1)*size;
        List<Student> stuList = sd.selectStudentPage(start, size, student);

        //将获取的数据封装为page对象
        Page<Student> page = new Page<>(size, current, (int) count, stuList);
        return page;
    }

    @Override
    public int deleStudentByIds(String ids) {
        int num = 0;
        if (ids != null && ids.trim() != ""){
            // 将学生编号列表拆分成单个 学生编号 的数组
            String[] sid = ids.split(",");

            for (String id : sid) {
                int idd = Integer.parseInt(id);
                // 调用到层方法
                num += sd.deleteStudentById(idd);
            }
        }
        return num;
    }

    @Override
    public Student updateStudentByIds(String id) {
        if (id != null && id.trim() != ""){
            int sid = Integer.parseInt(id);
            Student student = sd.getStudentById(sid);
            return student;
        }
        return null;
    }

    @Override
    public boolean updataStudentDo(Student student) {
       int row =  sd.updataStudentDo(student);
        return row>0;
    }
}
